home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / dfpp01.zip / TIMER.CPP < prev    next >
C/C++ Source or Header  |  1992-11-21  |  1KB  |  54 lines

  1. // -------- timer.cpp
  2.  
  3. #include <dos.h>
  4. #include "timer.h"
  5.  
  6. #ifdef MSC
  7. void (interrupt *Timer::OldTimer)(INTERRUPTARGS);
  8. #else
  9. void interrupt (*Timer::OldTimer)(INTERRUPTARGS);
  10. #endif
  11.  
  12. Timer *Timer::timers[MAXTIMERS];
  13. int Timer::timerct = 0;
  14.  
  15. // ------- timer interrupt service routine
  16. void interrupt NewTimer(INTERRUPTARGS)
  17. {
  18.     // --- countdown all running timers
  19.     for (int i = 0; i < MAXTIMERS; i++)
  20.         if (Timer::timers[i] != 0)
  21.             if (Timer::timers[i]->TimerRunning())
  22.                 Timer::timers[i]->Countdown();
  23.     // --- chain to the old interrupt vector
  24.     (*Timer::OldTimer)();
  25. }
  26.  
  27. Timer::Timer()
  28. {
  29.     timer = -1;
  30.     // --- if first timer, hook and chain the interrupt vector
  31.     if (timerct == 0)    {
  32.         OldTimer = getvect(TIMER);
  33.         setvect(TIMER, NewTimer);
  34.     }
  35.     // --- add the timer to the table
  36.     timers[timerct++] = this;
  37. }
  38.  
  39. Timer::~Timer()
  40. {
  41.     // --- remove the timer from the table
  42.     for (int i = 0; i < MAXTIMERS; i++)    {
  43.         if (this == timers[i])    {
  44.             timers[i] = 0;
  45.             break;
  46.         }
  47.     }
  48.     // --- if last one, restore the interrupt vector
  49.     if (--timerct == 0)
  50.         setvect(TIMER, OldTimer);
  51. }
  52.  
  53.  
  54.